﻿//-----------------------------------------------------------------------------
// Copyright (c) BaFang Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

namespace BaFangSpider
{

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.IO;

    /// <summary>
    /// Summary description for Logger
    /// </summary>
    public class Logger
    {
        private string logFilePath;
        private static Logger instance;

        private Logger()
        {
            this.logFilePath = Path.Combine(DataManager.BaseFilePath, "log.txt");
            if (File.Exists(this.logFilePath))
            {
                File.Delete(logFilePath);
            }
            File.CreateText(this.logFilePath).Close();
        }

        static public Logger GetLogger()
        {
            if (instance == null)
            {
                instance = new Logger();
            }
            return instance;
        }

        public void LogException(Exception e)
        {
            this.Write("!!!Exception:" + e.Message + e.StackTrace);
        }

        public void Write(string log)
        {
            object o = new object();
            lock (o)
            {
                try
                {
                    StreamWriter writer = File.AppendText(this.logFilePath);
                    writer.WriteLine(log);
                    writer.Flush();
                    writer.Close();
                }
                catch
                {
                }
            }
        }
    }
}